name: VyOS Build (Release)

on:
  push:
    tags:
      - "*"

jobs:
  build:

    runs-on: ubuntu-latest
    container:
      # For VyOS 1.2 (crux) use vyos/vyos-build:crux
      # For VyOS 1.3 (equuleus) use vyos/vyos-build:equuleus
      # For our VyOS rolling release you should use vyos/vyos-build which will always refer to the latest image.
      # Ref: https://docs.vyos.io/en/latest/contributing/build-vyos.html#build
      image: vyos/vyos-build:equuleus
      env:
        TZ: Etc/UTC
      options: --privileged

    permissions:
      actions: read
      checks: read
      contents: write
      deployments: write
      issues: write
      packages: write
      pull-requests: write
      repository-projects: write
      security-events: write
      statuses: write

    steps:
    - name: Set env
      run: |
        echo "RELEASE_VERSION=${GITHUB_REF:11}" >> $GITHUB_ENV

    - name: git clone vyos-build
      run: |
        set -eux

        git clone -b equuleus --single-branch https://github.com/vyos/vyos-build

    - name: configure
      working-directory: vyos-build
      run: |
        set -eux

        ./configure \
          --architecture amd64 \
          --build-by yamadharma@gmail.com \
          --build-type release \
          --version ${{ env.RELEASE_VERSION }}

    - name: make iso
      working-directory: vyos-build
      run: |
        set -eux

        make iso

    # - name: make qemu
    #   working-directory: vyos-build
    #   run: |
    #     set -eux

    #     make qemu

    - name: ls
      run: |
        set -eux

        pwd
        ls -lah
        ls -lah vyos-build/build

    - uses: actions/create-release@master
      id: create_release
      env:
        GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
      with:
        tag_name: ${{ github.ref }}
        release_name: VyOS ${{ github.ref }} 
        body: |
          VyOS ${{ env.RELEASE_VERSION }}
        draft: true
        prerelease: true

    - uses: actions/upload-release-asset@master
      env:
        GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
      with:
        #
        upload_url: ${{ steps.create_release.outputs.upload_url }}
        #
        asset_path: vyos-build/build/vyos-${{ env.RELEASE_VERSION }}-amd64.iso
        #
        asset_name: vyos-${{ env.RELEASE_VERSION }}-amd64.iso
        #
        asset_content_type: application/x-iso9660-image

    # - uses: actions/upload-release-asset@master
    #   env:
    #     GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
    #   with:
    #     #
    #     upload_url: ${{ steps.create_release.outputs.upload_url }}
    #     #
    #     asset_path: vyos-build/build/vyos-${{ env.RELEASE_VERSION }}-amd64.qcow2
    #     #
    #     asset_name: vyos-${{ env.RELEASE_VERSION }}-amd64.qcow2
    #     #
    #     asset_content_type: application/x-qemu-disk
